package com.lanren.excel;

import com.lanren.utils.FileTranscodingUtils;
import com.lanren.utils.fastcsv.reader.CsvContainer;
import com.lanren.utils.fastcsv.reader.CsvParser;
import com.lanren.utils.fastcsv.reader.CsvReader;
import com.lanren.utils.fastcsv.reader.CsvRow;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class TestCSV {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\uploads\\doc\\微信支付账单(20190809-20190909).csv");
        System.out.println("文件编码:" + FileTranscodingUtils.getInstance().guessFileEncoding(file));
        String eode = FileTranscodingUtils.getInstance().guessFileEncoding(file);
        if (!eode.equals("UTF-8")) {
            // 封装目录,需要修改文件格式的路径
            FileTranscodingUtils.getInstance().getFilePaths(file);
        }
        csvReadOperation(file);
//        File file = new File("D:\\MyDownload\\微信支付账单(20190809-20190909)\\微信支付账单(20190809-20190909).csv");
//        File file = new File("D:\\MyDownload\\alipay_record_20190909_1042\\alipay_record_20190909_1042_1.csv");
//        getWeChat(file);
//        getAlipay(file);

    }

    /*private static void getWeChat(File file) {
        try {
            System.out.println("文件编码:" + FileTranscodingUtils.getInstance().guessFileEncoding(file));
            String eode = FileTranscodingUtils.getInstance().guessFileEncoding(file);
            if (!eode.equals("UTF-8")) {
                // 封装目录,需要修改文件格式的路径
                FileTranscodingUtils.getInstance().getFilePaths(file);
            }
            List<WeChat> weChatList = ExcelUtils.getInstance().readCSV2Objects(file.getAbsolutePath(), WeChat.class);
            for (WeChat weChat : weChatList) {
                System.out.println(weChat);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void getAlipay(File file) {
        try {
            System.out.println("文件编码:" + FileTranscodingUtils.getInstance().guessFileEncoding(file));
            String eode = FileTranscodingUtils.getInstance().guessFileEncoding(file);
            if (!eode.equals("UTF-8")) {
                // 封装目录,需要修改文件格式的路径
                FileTranscodingUtils.getInstance().getFilePaths(file);
            }

            List<AliPay> aliPayList = ExcelUtils.getInstance().readCSV2Objects(file.getAbsolutePath(), AliPay.class);
            for (AliPay aliPay : aliPayList) {
                System.out.println(aliPay);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

    public static void csvReadOperation(File file) throws IOException {
        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);//
        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
        System.out.println("First column of line Count: " + csv.getRowCount());

        for (int i = 16; i < csv.getRows().size(); i++) {
            CsvRow row = csv.getRow(i);
            System.out.println("First column of line: " + row.getField(0));
        }
      /*  for (CsvRow row : csv.getRows()) {
            System.out.println("First column of h Count: " + row.getFieldCount());
            System.out.println("First column of line: " + row.getField(0));
        }*/
    }

    public static void csvReadOperation1() throws IOException {
        File file = new File("F:\\Excel\\customer-info.csv");
        CsvReader csvReader = new CsvReader();

        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
        for (CsvRow row : csv.getRows()) {
            System.out.println("Read line: " + row);
            System.out.println("First column of line: " + row.getField(0));
            if (row.getOriginalLineNumber() != 1) {
//                ResultInfo resultInfo = new ResultInfo();
//                resultInfo.setUserId(row.getField(0));
//                resultInfo.setUserPhone(row.getField(1));
//                resultInfo.setUserName(row.getField(2));
//                resultInfo.setRegistTime(row.getField(3));
//                resultInfo.setUserlevel(row.getField(4));
//                resultInfo.setAppName(row.getField(10));
//                listResultInfo.add(resultInfo);
            }

        }
    }

    public static void csvReadOperation2() throws IOException {
        File file = new File("F:\\Excel\\customer-info.csv");
        CsvReader csvReader = new CsvReader();

        try (CsvParser csvParser = csvReader.parse(file, StandardCharsets.UTF_8)) {
            CsvRow row;
            while ((row = csvParser.nextRow()) != null) {
                System.out.println("Read line: " + row);
                System.out.println("First column of line: " + row.getField(0));
            }
        }
    }
}
